#include "model/model_dir.h"
//#include "../include/model_dir.h"
#include "model/mysqlhelper.h"
//#include "../include/mysqlhelper.h"
#include <mysql/field_types.h>
#include <mysql/mysql.h>
#include <mysql/mysql_com.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int selectDirByid(MYSQL *conn, int id, DIR_M *dir){

    if(dir == NULL){
        return -1;
    } 

    // 绑定参数
    char sql[512] = "select * from dir_7th where id=?;";

    MYSQL_STMT *stmt;
    if(initStmt(&stmt,conn,sql)){
        printf("init stmt_preper error\n");
        return 1;
    }


    MYSQL_BIND inbind[1],outbind[8];
    memset(inbind,0,sizeof(inbind));
    memset(outbind,0,sizeof(outbind));

    // 绑定id参数
    initinBindParams(inbind, MYSQL_TYPE_LONG, (void *)&id, sizeof(int));
    if(mysql_stmt_bind_param(stmt,inbind)){
        fprintf(stderr, "mysql_stmt_inbind_param() failed: %s\n",
                mysql_stmt_error(stmt));
        mysql_stmt_close(stmt);
        return -1;
    }
    
    //执行语句
    if (mysql_stmt_execute(stmt)) {
        fprintf(stderr, "mysql_stmt_execute() failed: %s\n",
                mysql_stmt_error(stmt));
        mysql_stmt_close(stmt);
        return 1;
    }

    initBindResult(outbind    , MYSQL_TYPE_LONG, &dir->id, -1, NULL, NULL, NULL);
    initBindResult(outbind + 1, MYSQL_TYPE_LONG, &dir->user_id, -1, NULL, NULL, NULL);
    initBindResult(outbind + 2, MYSQL_TYPE_LONG, &dir->file_id, -1, NULL, NULL, NULL);
    initBindResult(outbind + 3, MYSQL_TYPE_STRING,dir->file_name, 255, NULL, NULL, NULL);
    initBindResult(outbind + 4, MYSQL_TYPE_LONG, &dir->parent_id, -1, NULL, NULL, NULL);
    initBindResult(outbind + 5, MYSQL_TYPE_STRING, dir->type, 3, NULL, NULL, NULL);
    initBindResult(outbind + 6, MYSQL_TYPE_STRING, dir->path, 255, NULL, NULL, NULL);
    initBindResult(outbind + 7, MYSQL_TYPE_LONG, &dir->is_deleted, 0, NULL, NULL, NULL);

    if (mysql_stmt_bind_result(stmt, outbind)) {
        fprintf(stderr, " mysql_stmt_bind_result() failed\n");
        fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
        return -1;
    }
    // 处理接收数据
    if (mysql_stmt_store_result(stmt)) {
        fprintf(stderr, " mysql_stmt_store_result() failed\n");
        fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
        exit(0);
    }

    if(mysql_stmt_fetch(stmt)){
        printf("mysql_stmt_fetch error\n");
        return -1;
    }

    mysql_stmt_close(stmt);
    return 0;
}

// 返回-1表示参数不正确,1是系统失败，不符合表约束、初始失败等
int insertDir(MYSQL *conn,const DIR_M *dir, int *ret_id){

    char sql[512] = "insert into dir_7th(user_id,file_id,file_name,parent_id,type,path,is_deleted) value(?,?,?,?,?,?,?);";

    MYSQL_STMT *stmt;
    
    MYSQL_BIND inbind[7];

    if (initStmt(&stmt, conn, sql)) {
        printf("init stmt_preper error\n");
        return 1;
    }

    memset(inbind, 0, sizeof(inbind));
    initinBindParams(inbind    , MYSQL_TYPE_LONG, (void *)&dir->user_id,
                     sizeof(int));
    initinBindParams(inbind + 1, MYSQL_TYPE_LONG, (void *)&dir->file_id,
                    sizeof(int));
    initinBindParams(inbind + 2, MYSQL_TYPE_STRING, (void *)dir->file_name,
                     strlen(dir->file_name));
    initinBindParams(inbind + 3, MYSQL_TYPE_LONG, (void *)&dir->parent_id,
                    sizeof(int));
    initinBindParams(inbind + 4, MYSQL_TYPE_STRING, (void *)dir->type,
                     strlen(dir->type));
    initinBindParams(inbind + 5, MYSQL_TYPE_STRING, (void *)dir->path,
                     strlen(dir->path));
    initinBindParams(inbind + 6, MYSQL_TYPE_LONG, (void *)&dir->is_deleted,
                    sizeof(int));

    // 绑定参数到预编译语句
    if (mysql_stmt_bind_param(stmt, inbind)) {
        fprintf(stderr, "mysql_stmt_inbind_param() failed: %s\n",
                mysql_stmt_error(stmt));
        mysql_stmt_close(stmt);
        return -1;
    }

    // 执行预编译语句
    if (mysql_stmt_execute(stmt)) {
        fprintf(stderr, "mysql_stmt_execute() failed: %s\n",
                mysql_stmt_error(stmt));
        mysql_stmt_close(stmt);
        return 1;
    }



    // 把上次插入的id返回
    *ret_id = mysql_stmt_insert_id(stmt);

    mysql_stmt_close(stmt);
    return 0;

}

//
int deleteDir(MYSQL *conn,const DIR_M *dir){
    if(dir==NULL){
        return -1;
    }

    char sql[1024] = {0};

    if(dir->id != 0)
        sprintf(sql, "update dir_7th set is_deleted = %d where id =%d;", 1, dir->id);
    else {
        if(strlen(dir->file_name))
            sprintf(sql, "update dir_7th set is_deleted = %d where file_name= '%s';", 1, dir->file_name);
        else
            return -1; 
    }  

    if(mysql_query(conn, sql)) {
        printf("mysql_query error:%s\n", mysql_error(conn));
        return -1;
    }
    if(mysql_affected_rows(conn) == 0)
        return -1;
    return 0;

}

int selectDirByparent_id_mul(MYSQL *conn,int parent_id,DIR_M **dir,int *line_num){

    if(dir == NULL){
        return -1;
    } 

    // 绑定参数
    char sql[512] = "select * from dir_7th where parent_id=? and is_deleted=0;";

    MYSQL_STMT *stmt;
    if(initStmt(&stmt,conn,sql)){
        printf("init stmt_preper error\n");
        return 1;
    }



    MYSQL_BIND inbind[1],outbind[8];
    memset(inbind,0,sizeof(inbind));
    memset(outbind,0,sizeof(outbind));

    // 绑定id参数
    initinBindParams(inbind, MYSQL_TYPE_LONG, (void *)&parent_id, sizeof(int));
    if(mysql_stmt_bind_param(stmt,inbind)){
        fprintf(stderr, "mysql_stmt_inbind_param() failed: %s\n",
                mysql_stmt_error(stmt));
        mysql_stmt_close(stmt);
        return -1;
    }

    //执行语句
    if (mysql_stmt_execute(stmt)) {
        fprintf(stderr, "mysql_stmt_execute() failed: %s\n",
                mysql_stmt_error(stmt));
        mysql_stmt_close(stmt);
        return 1;
    }

    // 用来临时保存每一行的结果，最终要装到**dir
    DIR_M dir_temp = {0};

    initBindResult(outbind    , MYSQL_TYPE_LONG, &dir_temp.id, -1, NULL, NULL, NULL);
    initBindResult(outbind + 1, MYSQL_TYPE_LONG, &dir_temp.user_id, -1, NULL, NULL, NULL);
    initBindResult(outbind + 2, MYSQL_TYPE_LONG, &dir_temp.file_id, -1, NULL, NULL, NULL);
    initBindResult(outbind + 3, MYSQL_TYPE_STRING,dir_temp.file_name, 255, NULL, NULL, NULL);
    initBindResult(outbind + 4, MYSQL_TYPE_LONG, &dir_temp.parent_id, -1, NULL, NULL, NULL);
    initBindResult(outbind + 5, MYSQL_TYPE_STRING, dir_temp.type, 3, NULL, NULL, NULL);
    initBindResult(outbind + 6, MYSQL_TYPE_STRING, dir_temp.path, 255, NULL, NULL, NULL);
    initBindResult(outbind + 7, MYSQL_TYPE_LONG, &dir_temp.is_deleted, 0, NULL, NULL, NULL);

    if (mysql_stmt_bind_result(stmt, outbind)) {
        fprintf(stderr, " mysql_stmt_bind_result() failed\n");
        fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
        return -1;
    }
    // 处理接收数据
    if (mysql_stmt_store_result(stmt)) {
        fprintf(stderr, " mysql_stmt_store_result() failed\n");
        fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
        exit(0);
    }
    
    // 将结果装到**dir
    *line_num = mysql_stmt_num_rows(stmt);
    *dir = (DIR_M*)calloc(*line_num,sizeof(DIR_M));
    
    for(int i = 0; i<*line_num && !mysql_stmt_fetch(stmt);++i){
        (*dir)[i].id = dir_temp.id;
        (*dir)[i].user_id = dir_temp.user_id;
        (*dir)[i].file_id = dir_temp.file_id;
        strcpy((*dir)[i].file_name,dir_temp.file_name);
        (*dir)[i].parent_id = dir_temp.parent_id;
        strcpy((*dir)[i].type,dir_temp.type);
        strcpy((*dir)[i].path,dir_temp.path);
        (*dir)[i].is_deleted = dir_temp.is_deleted;
    }

    mysql_stmt_close(stmt);
    return 0;
}

// 没改，里面和selectDirByid一样，别用
//int selectDirByparent_id(MYSQL *conn, int id, DIR_M *dir){
//
//    if(dir == NULL){
//        return -1;
//    } 
//
//    // 绑定参数
//    char sql[512] = "select * from dir_7th where id=?;";
//
//    MYSQL_STMT *stmt;
//    if(initStmt(&stmt,conn,sql)){
//        printf("init stmt_preper error\n");
//        return 1;
//    }
//
//    printf("prepare SQL 语句准备成功\n");
//
//    MYSQL_BIND inbind[1],outbind[7];
//    memset(inbind,0,sizeof(inbind));
//    memset(outbind,0,sizeof(outbind));
//
//    // 绑定id参数
//    initinBindParams(inbind, MYSQL_TYPE_LONG, (void *)&id, sizeof(int));
//    if(mysql_stmt_bind_param(stmt,inbind)){
//        fprintf(stderr, "mysql_stmt_inbind_param() failed: %s\n",
//                mysql_stmt_error(stmt));
//        mysql_stmt_close(stmt);
//        return -1;
//    }
//    printf("参数绑定成功\n");
//    
//    //执行语句
//    if (mysql_stmt_execute(stmt)) {
//        fprintf(stderr, "mysql_stmt_execute() failed: %s\n",
//                mysql_stmt_error(stmt));
//        mysql_stmt_close(stmt);
//        return 1;
//    }
//    printf("预编译语句执行成功\n");
//
//    initBindResult(outbind    , MYSQL_TYPE_LONG, &dir->id, -1, NULL, NULL, NULL);
//    initBindResult(outbind + 1, MYSQL_TYPE_LONG, &dir->user_id, -1, NULL, NULL, NULL);
//    initBindResult(outbind + 2, MYSQL_TYPE_LONG, &dir->file_id, -1, NULL, NULL, NULL);
//    initBindResult(outbind + 3, MYSQL_TYPE_STRING,dir->file_name, 255, NULL, NULL, NULL);
//    initBindResult(outbind + 4, MYSQL_TYPE_LONG, &dir->parent_id, -1, NULL, NULL, NULL);
//    initBindResult(outbind + 5, MYSQL_TYPE_STRING, dir->type, 3, NULL, NULL, NULL);
//    initBindResult(outbind + 6, MYSQL_TYPE_STRING, dir->path, 255, NULL, NULL, NULL);
//
//    if (mysql_stmt_bind_result(stmt, outbind)) {
//        fprintf(stderr, " mysql_stmt_bind_result() failed\n");
//        fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
//        return -1;
//    }
//    // 处理接收数据
//    if (mysql_stmt_store_result(stmt)) {
//        fprintf(stderr, " mysql_stmt_store_result() failed\n");
//        fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
//        exit(0);
//    }
//
//    if(mysql_stmt_fetch(stmt)){
//        printf("mysql_stmt_fetch error\n");
//        return -1;
//    }
//
//    mysql_stmt_close(stmt);
//    return 0;
//}

int getDir_idByPid_Path(MYSQL *conn, int user_id, char path[], int *id) {
	char sql[1024] = {0};
	MYSQL_ROW row;
	MYSQL_RES *res;
	sprintf(sql, "select id from dir_7th where user_id = %d and path = '%s';", user_id, path);
	if(mysql_query(conn, sql)) {
		fprintf(stderr, "msql_qurty :%s\n", mysql_error(conn));
		return -1;
	};

	res = mysql_store_result(conn);
	if(res == NULL) {
		printf(" mysql_store_result error:%s\n", mysql_error(conn));
		return -1;
	}

	if((row = mysql_fetch_row(res)) == NULL) {
		printf("mysql_fetch_row null data\n");
		return 0;
	}
    *id=atoi(row[0]);
    mysql_free_result(res);
    return 0;
}

int getDir_ByPid_File_Name(MYSQL *conn,int cur_dir_id,char name[],DIR_M *dir){
    char sql[1024] = {0};
	MYSQL_ROW row;
	MYSQL_RES *res;
	sprintf(sql, "select id,file_id,type from dir_7th where parent_id = %d and file_name = '%s';", cur_dir_id,name);
	if(mysql_query(conn, sql)) {
		fprintf(stderr, "msql_qurty :%s\n", mysql_error(conn));
		return -1;
	};

	res = mysql_store_result(conn);
	if(res == NULL) {
		printf(" mysql_store_result error:%s\n", mysql_error(conn));
		return -1;
	}

	if((row = mysql_fetch_row(res)) == NULL) {
		printf("mysql_fetch_row null data\n");
		return 0;
	}
    dir->id=atoi(row[0]);
    dir->file_id=atoi(row[1]);
    strcpy(dir->type,row[2]);
    mysql_free_result(res);
    return 0;
}